package org.hoyi.dispatchfact;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hoyi.DB.conf.HOYIConf;
import org.hoyi.DB.ctrl.Console;
import org.hoyi.util.HTMLUtils;
import org.hoyi.util.StreamUtils;
import org.hoyi.wb.comment.RequestType;

/**
 * 通用文件的Dispatcher.
 * 
 * @author SAM
 *
 */
public class CustomFileDispatcher implements IDispatcher {
	
	/**
	 * 页面的缓存.
	 */
	private static Map<String, byte[]> customfilecache = new HashMap<>();
	private static Map<String, String> customfilesuffix = new HashMap<String, String>();

	@Override
	public boolean doSth(HttpServletRequest _request, HttpServletResponse _response, String _requrl, RequestType _reqType) {
		try {
			if (_requrl.endsWith(HOYIConf.Suffix)) {
				return false;
			}
			//Console.Info("_requrl:WebRoot" + _requrl);
			if (_requrl.trim().length() > 0 && _requrl.contains(".") && !_requrl.toLowerCase().endsWith(".hoyip.html")) {
				String suffix = _requrl.substring(_requrl.lastIndexOf(".") + 1, _requrl.length());
				Console.Info("_requrl:" + _requrl);
				//Console.Info("suffix:" + suffix);
				
				String loadpath = _requrl;
				// 这里也用HTMLUtils里的方法来解决资源文件缓存的问题.
				if(HTMLUtils.OpenHTMLCache) {
					if(customfilecache.containsKey(_requrl)) {
						OutputStream outstream = _response.getOutputStream();
						String contype = customfilesuffix.get(_requrl);
						_response.setHeader("Content-Type", contype);
						
						outstream.write(customfilecache.get(_requrl));
						outstream.flush();
						outstream.close();
						return true;
					}
				}
				
				// 如果打开本地通用文件读取，则从本地读文件.
				if(HOYIConf.Local_Custom_File_Read_Open) {
					loadpath = HOYIConf.Local_Custom_File_Path + _requrl;
				}else {
					loadpath = "WebRoot" + _requrl;
				}
				
				byte[] bts;
				if(HOYIConf.Local_Custom_File_Read_Open) {
					try {
						bts = StreamUtils.Create().ReadLocalFileResource(loadpath);
					} catch (Exception e) {
						// 如果本地文件没有，则从包内读一次.
						bts = StreamUtils.Create().ReaderResource("WebRoot" + _requrl);
					}
				}else {
					bts = StreamUtils.Create().ReaderResource(loadpath);
				}
//				String content = new String(bts, "UTF-8");
				
				OutputStream outstream = _response.getOutputStream();
				String contype = this.GetTypeBySuffix(suffix);
				
				_response.setHeader("Content-Type", contype);
				outstream.write(bts, 0, bts.length);
				outstream.flush();
				outstream.close();
				
				customfilecache.put(_requrl, bts);
				customfilesuffix.put(_requrl, contype);
				return true;
			}
		} catch (Exception e) {
			Console.Error("---------> NO FILE:" + _requrl);
			customfilecache.put(_requrl, null);
			customfilesuffix.put(_requrl, "text/css");
		}
		return false;
	}

	public String GetTypeBySuffix(String suffix) {
		// 上传的文件统一用bat做后缀，文件库内存储bat文件的后缀，下载的时候再重写后缀.
		Map<String, String> suffixmap = new HashMap<String, String>();
		suffixmap.put("htm", "text/html; charset=UTF-8");
		suffixmap.put("html", "text/html; charset=UTF-8");
		suffixmap.put("json", "text/json; charset=UTF-8");
		suffixmap.put("text", "text/plain; charset=UTF-8");

		suffixmap.put("js", "application/javascript; charset=UTF-8");
		suffixmap.put("css", "text/css; charset=UTF-8");

		suffixmap.put("ico", "image/x-icon");

		suffixmap.put("jpg", "image/jpg");
		suffixmap.put("png", "image/png");
		suffixmap.put("gif", "image/gif");

		suffixmap.put("pdf", "application/pdf");
		suffixmap.put("word", "application/msword");
		
		suffixmap.put("woff2", "application/x-font-woff");
		suffixmap.put("woff", "application/x-font-woff");
		suffixmap.put("ttf", "font/ttf");
//		suffixmap.put("ttf", "application/octet-stream");
		
		suffixmap.put("apk", "application/vnd.android.package-archive");
		suffixmap.put("gzip", "application/x-gzip");
		suffixmap.put("gzip", "application/x-gzip");

		if (suffixmap.containsKey(suffix)) {
			return (String) suffixmap.get(suffix);
		}
		return "text/plain; charset=UTF-8";
	}

}
